package com.neuronbit.businessflow.intellij.lang;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiType;
import com.intellij.refactoring.changeSignature.ParameterInfo;
import com.neuronbit.businessflow.intellij.psi.SimpleElementFactory;
import org.antlr.intellij.adaptor.psi.ANTLRPsiNode;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import static com.neuronbit.businessflow.intellij.lang.BpmParserDefinition.PARAM;

/**
 * The type Psi java method call.
 */
public class PsiJavaMethodCall extends ANTLRPsiNode {
    /**
     * Instantiates a new Psi java method call.
     *
     * @param node the node
     */
    public PsiJavaMethodCall(@NotNull ASTNode node) {
        super(node);
    }

    /**
     * Sets method.
     *
     * @param newName the new name
     */
    public void setMethod(String newName) {
        System.out.println(newName);
        PsiElement newMethodAttr = SimpleElementFactory.createAttribute(this.getProject(), "method", newName);
        for (PsiElement child : this.getChildren()) {
            if (child instanceof PsiAttribute){
                if (((PsiAttribute) child).getAttrName().equalsIgnoreCase("method")) {
                    child.replace(newMethodAttr);
                    break;
                }
            }
        }
    }

    /**
     * Sets parameters.
     *
     * @param newParameters the new parameters
     */
    public void setParameters(ParameterInfo[] newParameters) {
        List<PsiElement> paramElements = this.findChildrenByType(PARAM);
        PsiElement newMethodAttr = SimpleElementFactory.createParamArray(this.getProject(), paramElements, newParameters);
        System.out.println(newParameters.length);
    }

    /**
     * Sets return type.
     *
     * @param returnType the return type
     */
    public void setReturnType(PsiType returnType) {
        System.out.println(returnType);
    }
}
